#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化的时间目录测试脚本
"""

import requests
import json
from datetime import datetime

def test_simple_upload_download():
    """简单的上传下载测试"""
    base_url = "http://127.0.0.1:8084"
    username = "testuser"
    password = "testpass123"
    
    session = requests.Session()
    session.auth = (username, password)
    
    test_file = "simple_test.txt"
    test_content = "简单测试内容"
    
    print(f"测试文件: {test_file}")
    print(f"测试内容: {test_content}")
    
    try:
        # 1. 上传文件
        print("\n1. 上传文件...")
        response = session.put(f"{base_url}/dav/{test_file}", data=test_content)
        print(f"上传响应状态码: {response.status_code}")
        print(f"上传响应内容: {response.text}")
        
        if response.status_code not in [200, 201, 204]:
            print("❌ 文件上传失败")
            return False
        
        # 2. 下载文件
        print("\n2. 下载文件...")
        response = session.get(f"{base_url}/dav/{test_file}")
        print(f"下载响应状态码: {response.status_code}")
        print(f"下载响应内容: {response.text}")
        
        if response.status_code == 200:
            if response.text == test_content:
                print("✅ 文件下载成功，内容正确")
                download_success = True
            else:
                print("❌ 文件下载成功，但内容不正确")
                download_success = False
        else:
            print("❌ 文件下载失败")
            download_success = False
        
        # 3. 删除文件
        print("\n3. 删除文件...")
        response = session.delete(f"{base_url}/dav/{test_file}")
        print(f"删除响应状态码: {response.status_code}")
        
        if response.status_code in [200, 204]:
            print("✅ 文件删除成功")
            delete_success = True
        else:
            print("❌ 文件删除失败")
            delete_success = False
        
        return download_success and delete_success
        
    except Exception as e:
        print(f"❌ 测试异常: {e}")
        return False

if __name__ == "__main__":
    print("🚀 开始简化时间目录测试")
    print("=" * 50)
    
    success = test_simple_upload_download()
    
    print("\n" + "=" * 50)
    if success:
        print("✅ 测试通过")
    else:
        print("❌ 测试失败")
